﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace mqjlink
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> argList = ArgParser(args);

            try
            {
                JlinkAPI.JLINKARM_Open();
                if (!JlinkAPI.JLINKARM_IsOpen())
                {
                    Console.WriteLine("Jlink未连接！");
                    return;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("未找到Jlink驱动，请检查程序是否完整。");
                return;
            }
            #region Jlink日志输出
            //if (argList.ContainsKey("-l"))
            //{
            //    LogComDelegate jlog = new LogComDelegate((str) =>
            //    {
            //        //Console.WriteLine(str.ToString());
            //        File.AppendAllText("JlinkLog.txt", str.ToString());
            //    });
            //    JlinkAPI.JLINKARM_EnableLogCom(jlog);
            //}
            #endregion
            

            JlinkAPI.JLINKARM_TIF_Select(1);//TIF = transport interface??? 1=swd???
            JlinkAPI.JLINKARM_ExecCommand("ProjectFile = " + (argList.ContainsKey("-s") ? argList["-s"] : "JLinkSettings.ini"), 0, 1000, 0);//JLinkSettings.ini
            JlinkAPI.JLINKARM_ExecCommand("device = " + (argList.ContainsKey("-d") ? argList["-d"] : "STM32F103C8"), 0, 1000, 0);
            JlinkAPI.JLINKARM_SetSpeed(1000);
            string filename = argList.ContainsKey("-f") ? argList["-f"] : "output.bin";
            if (File.Exists(filename))
            {
                System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);
                System.IO.BinaryReader read = new System.IO.BinaryReader(fs);
                if (!JlinkAPI.JLINKARM_IsHalted()) JlinkAPI.JLINKARM_Halt();
                JlinkAPI.JLINKARM_BeginDownload(0);
                JlinkAPI.JLINKARM_WriteMem((uint)(argList.ContainsKey("-o") ? Convert.ToInt32(argList["-o"], 16) : 0x08000000), (UInt32)fs.Length, read.ReadBytes((int)fs.Length));
                JlinkAPI.JLINKARM_EndDownload();
                read.Close();
                fs.Close();
                fs.Dispose();
                JlinkAPI.JLINKARM_Reset();
                JlinkAPI.JLINKARM_Go();

            }
            else
            {
                Console.WriteLine(string.Format("cannot file file {0}", filename));
            }
            JlinkAPI.JLINKARM_Close();
        }

        private static Dictionary<string, string> ArgParser(string[] args)
        {
            Dictionary<string, string> ParamList = new Dictionary<string, string>();
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    ParamList.Add(args[i], args[i + 1]);
                    i++;
                }
            }
            return ParamList;
        }
    }
}
